package icu.makka.yygh.user.api;

import icu.makka.yygh.model.user.Patient;
import icu.makka.yygh.result.Result;
import icu.makka.yygh.result.helper.AuthContextHolder;
import icu.makka.yygh.user.service.PatientService;
import icu.makka.yygh.user.service.impl.PatientServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientServiceImpl patientService;

    /**
     * 获取就诊人列表
     * @param request
     * @return
     */
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        log.info("获取就诊人列表");
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList = patientService.findAllByUserId(userId);
        return Result.ok(patientList);
    }

    /**
     * 添加就诊人
     * @param patient
     * @param request
     * @return
     */
    @PostMapping("/auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        log.info("添加就诊人，patient={}", patient);
        //获取当前登录用户
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return
     */
    @GetMapping("/auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        log.info("根据id获取就诊人信息，id={}", id);
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return
     */
    @PostMapping("/inner/get/{id}")
    public Patient getPatientFromOrder(@PathVariable Long id) {
        log.info("根据id获取就诊人信息，id={}", id);
        Patient patient = patientService.getPatientById(id);
        return patient;
    }

    /**
     * 修改就诊人
     * @param patient
     * @return
     */
    @PostMapping("/auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        log.info("修改就诊人，patient={}", patient);
        patientService.updateById(patient);
        return Result.ok();
    }

    /**
     * 删除就诊人
     * @param id
     * @return
     */
    @DeleteMapping("/auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {
        log.info("删除就诊人");
        patientService.removeById(id);
        return Result.ok();
    }

}
